package javax.validation.constraints.impl;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.IdCard;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 手机号码验证
 * 
 * @author leiwuluan
 *
 */
public class IdCardValidator implements ConstraintValidator<IdCard, String> {

	@Override
	public boolean isValid(String idCard, ConstraintValidatorContext constraintContext) {
		String str = "^[1-9]\\d{5}[1-9]\\d{3}((0[1-9])|(1[0-2]))(0[1-9]|([1|2][0-9])|3[0-1])((\\d{4})|\\d{3}X)$";
		Pattern p = Pattern.compile(str);
		boolean isValid = false;
		if(StringUtils.isNotBlank(idCard)) {
			Matcher m = p.matcher(idCard);
			isValid = m.matches();
		}
		return isValid;
	}

}
